package com.jiliang.jiliangserver.controller;



import com.jiliang.jiliangserver.annotation.Log;
import com.jiliang.jiliangserver.domain.*;
import com.jiliang.jiliangserver.enums.BusinessType;
import com.jiliang.jiliangserver.model.AjaxResult;
import com.jiliang.jiliangserver.model.page.TableDataInfo;
import com.jiliang.jiliangserver.service.MeterService;
import com.jiliang.jiliangserver.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jiliang")
@Slf4j
public class MeterController extends BaseController {

    @Autowired
    private MeterService meterService;

    /**
     * 获取计量总览统计数据
     * @return
     */
    @GetMapping("/getDeviceList")
    public AjaxResult getDeviceList(){
        Map map = meterService.selectMeterOverview();
        return AjaxResult.success(map);
    }

    /**
     * 获取异常统计水表详细信息
     * 实时异常统计
     * @return
     */
    @GetMapping("/exceptionInfoStatistics")
    public TableDataInfo exceptionInfoStatistics(MeterQuery query){
        startPage();
        List<Map> maps = meterService.exceptionInfoStatistics(query);
        return getDataTable(maps);
    }

    /**
     * 获取异常统计各类异常数量
     * @return
     */
    @GetMapping("/selectMeterExcCount")
    public AjaxResult selectMeterExcCount(MeterQuery query){
        Map map = meterService.exceptionSumStatistics(query);
        return AjaxResult.success(map);
    }

    /**
     * 资料统计
     * @return
     */
    @GetMapping("/meterInfoStatistics")
    public TableDataInfo meterInfoStatistics(MeterQuery query){
        startPage();
        List<Map> maps = meterService.meterInfoStatistics(query);
        return getDataTable(maps);
    }


    /**
     * 小区用水
     * @return
     */
    @GetMapping("/selectDiWater")
    public TableDataInfo selectDiWater(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectDiWater(query);
        return getDataTable(maps);
    }
    /**
     * 小区阶梯用水
     * @return
     */
    @GetMapping("/selectXqStair")
    public AjaxResult selectXqStair(MeterQuery query){
        Map map = meterService.selectXqStair(query);
        return AjaxResult.success(map);
    }

    /**
     * 根据月份查询抄表数据
     * @return
     */
    @GetMapping("/selectMeterReadInfo")
    public TableDataInfo selectMeterReadInfo(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectMeterReadInfo(query);
        return getDataTable(maps);
    }


    /**
     * 查询水表所有抄表记录
     * @return
     */
    @GetMapping("/selectCbByMiid")
    public TableDataInfo selectCbByMiid(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectCbByMiid(query);
        return getDataTable(maps);
    }

    /**
     * 查询小区近一年抄表情况（按月）
     * @return
     */
    @GetMapping("/selectXqCbByMonth")
    public AjaxResult selectXqCbByMonth(MeterQuery query){
        List<Map> maps = meterService.selectXqCbByMonth(query);
        return AjaxResult.success(maps);
    }


    /**
     * 报警设置
     * @return
     */
    @PostMapping("/alarmSetting")
    public AjaxResult alarmSetting(@RequestBody MeterDto meterDto){
        meterService.alarmSetting(meterDto);
        return AjaxResult.success();
    }

    /**
     * 条件查询水表
     * @return
     */
    @GetMapping("/selectMeterInfoList")
    public TableDataInfo selectMeterInfoList(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectMeterInfoList(query);
        return getDataTable(maps);
    }

    /**
     * 用户用水
     * @return
     */
    @GetMapping("/selectUserWater")
    public TableDataInfo selectUserWater(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectUserWater(query);
        return getDataTable(maps);
    }


    /**
     * 用户阶梯水量查询
     * @return
     */
    @GetMapping("/selectUserStair")
    public AjaxResult selectUserStair(MeterQuery query){
        Map map = meterService.selectUserStair(query);
        return AjaxResult.success(map);
    }

    /**
     * 查询抄表历史（历史账表）
     * @return
     */
    @GetMapping("/selectCbHistory")
    public TableDataInfo selectCbHistory(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectCbHistory(query);
        return getDataTable(maps);
    }

    /**
     * 查询抄表历史（meterread_his表）
     * @return
     */
    @GetMapping("/selectMeterReadHis")
    public TableDataInfo selectMeterReadHis(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectMeterReadHis(query);
        return getDataTable(maps);
    }

    /**
     * 查询各小区抄表情况
     * @return
     */
    @GetMapping("/selectXqCbState")
    public TableDataInfo selectXqCbState(MeterQuery query){
        startPage();
        //如果查询当月，mrmonth字段应设置为空
        //查询往期数据mrmonth不能为空，且不能为当月
        List<Map> maps = meterService.selectXqCbState(query);
        return getDataTable(maps);
    }

    /**
     * 查询集中器抄表情况
     * @return
     */
    @GetMapping("/selectConcentratorState")
    public TableDataInfo selectConcentratorState(MeterQuery query){
        startPage();
        //如果查询当月，mrmonth字段应设置为空
        //查询往期数据mrmonth不能为空，且不能为当月
        List<Map> maps = meterService.selectConcentratorState(query);
        return getDataTable(maps);
    }

    /**
     * 用户明细
     * 户名 地址 用水量 分公司 抄表月
     * @return
     */
    @GetMapping("/selectCiWaterDetail")
    public TableDataInfo selectCiWaterDetail(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectCiWaterDetail(query);
        return getDataTable(maps);
    }


    /**
     * 大表首页
     * 查询大表小表水表明细
     * @return
     */
    @GetMapping("/selectMeterDetail")
    public TableDataInfo selectMeterDetail(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectMeterDetail(query);
        return getDataTable(maps);
    }

    /**
     * 查询大表小表用水总量
     * @return
     */
    @GetMapping("/selectMeterWaterSum")
    public AjaxResult selectMeterWaterSum(MeterQuery query){
        Map result = meterService.selectMeterWaterSum(query);
        return AjaxResult.success(result);
    }


    /**
     * 换表分析
     * @return
     */
    @GetMapping("/changeMeterAnalysis")
    public TableDataInfo changeMeterAnalysis(MeterQuery query){
        startPage();
        List<Map> maps = meterService.changeMeterAnalysis(query);
        return getDataTable(maps);
    }


    /**
     * 用水趋势
     * @return
     */
    @GetMapping("/selectUserRecentWater")
    public AjaxResult selectUserRecentWater(MeterQuery query){
        List<Map> maps = meterService.selectUserRecentWater(query);
        return AjaxResult.success(maps);
    }


    /**
     * 同比环比分析
     * @return
     */
    @GetMapping("/monthYearCompare")
    public AjaxResult monthYearCompare(MeterQuery query){
        List<Map> maps = meterService.monthYearCompare(query);
        return AjaxResult.success(maps);
    }

    /**
     * 查询用户列表信息
     * @return
     */
    @GetMapping("/selectCustinfoList")
    public TableDataInfo selectCustinfoList(MeterQuery query){
        startPage();
        List<Custinfo> maps = meterService.selectCustinfoList(query);
        return getDataTable(maps);
    }

    /**
     * 导出用户列表信息
     * @return
     */
    @GetMapping("/ExportCustinfoList")
    public void ExportCustinfoList(HttpServletResponse response, MeterQuery query){
        List<Custinfo> maps = meterService.selectCustinfoList(query);
        ExcelUtil<Custinfo> util = new ExcelUtil<Custinfo>(Custinfo.class);
        util.exportExcel(response, maps, "字典类型");
    }

    /**
     * 查询拆装(销户)工单
     * @return
     */
    @GetMapping("/selectRequestXhList")
    public TableDataInfo selectRequestXhList(RequestXh requestXh){
        startPage();
        List<RequestXh> requestXhs = meterService.selectRequestXhList(requestXh);
        return getDataTable(requestXhs);
    }

    /**
     * 故障换表工单查询
     * @return
     */
    @GetMapping("/selectRequestGzhbList")
    public TableDataInfo selectRequestGzhbList(RequestGzhb gzhb){
        startPage();
        List<RequestGzhb> gzhbList = meterService.selectRequestGzhbList(gzhb);
        return getDataTable(gzhbList);
    }


    /**
     * 核实异常表
     * @param miid
     * @return
     */
    @PutMapping("/exceptionComfirm")
    public AjaxResult exceptionComfirm(String miid){
        meterService.exceptionComfirm(miid);
        return AjaxResult.success();
    }


    /**
     * 查询各类监控数量
     * @return
     */
    @GetMapping("/selectMonitorCounts")
    public AjaxResult selectMonitorCounts(){
        Map map = meterService.selectMonitorCounts();
        return AjaxResult.success(map);
    }




    /**
     * 创建集中器
     * @param concentrator
     * @return
     */
    @Log(title = "创建集中器", businessType = BusinessType.INSERT)
    @PostMapping("/createConcentrator")
    public AjaxResult createYcTemplate(@RequestBody Concentrator concentrator) throws Exception{
        meterService.createConcentrator(concentrator);
        return AjaxResult.success();
    }


    /**
     * 根据id查询集中器
     * @param id
     * @return
     */
    @GetMapping("/selectConcentratorById")
    public AjaxResult selectConcentratorById(Long id) throws Exception{
        Concentrator concentrator = meterService.selectConcentratorById(id);
        return AjaxResult.success(concentrator);
    }

    /**
     * 条件查询集中器
     * @param
     * @return
     */
    @GetMapping("/selectConcentratorList")
    public TableDataInfo selectConcentratorList(Concentrator concentrator) throws Exception{
        startPage();
        List<Concentrator> concentratorList = meterService.selectConcentratorList(concentrator);
        return getDataTable(concentratorList);
    }

    /**
     * 修改集中器
     * @param
     * @return
     * @throws Exception
     */
    @Log(title = "修改集中器", businessType = BusinessType.UPDATE)
    @PutMapping("/updateConcentrator")
    public AjaxResult updateConcentrator(@RequestBody Concentrator concentrator) throws Exception{
        meterService.updateConcentrator(concentrator);
        return AjaxResult.success();
    }



    /**
     * 查询小区
     * @return
     */
    @GetMapping("/selectDistrictinfo")
    public TableDataInfo selectDistrictinfo(MeterQuery query){
        startPage();
        List<Map> maps = meterService.selectDistrictinfo(query);
        return getDataTable(maps);
    }


    /**
     * 查询营业所用水情况
     * @return
     */
    @GetMapping("/selectWaterTrendByDept")
    public AjaxResult selectWaterTrendByDept(MeterQuery query) {
        List<Map> maps = meterService.selectWaterTrendByDept(query);
        return AjaxResult.success(maps);
    }




}






